package com.menghanjun.mhjresourcesharing.utils;

import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.util.Enumeration;

public class CombinedIdGenerator {
    public static long generateId() {
        long timestamp = System.currentTimeMillis();
        int machineId = getMachineIdentifier(); // Replace this with your logic

        long id = (timestamp << 20) | machineId;
        return id;
    }

    private static int getMachineIdentifier() {
        int machineId = 0;
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface ni = networkInterfaces.nextElement();
                byte[] mac = ni.getHardwareAddress();
                if (mac != null) {
                    machineId = MessageDigest.getInstance("MD5").digest(mac)[0] & 0xFF;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return machineId;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            long id = generateId();
            System.out.println("Generated ID: " + id);
        }
    }
}
